// *** LICENSE HEADER ***
// Filename: ./mainmenu.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "mainmenu.h"

mainMenu::mainMenu(engine* newParent) {
    parent = newParent;
    timestamp = 0;
    titleTexture = 0;
    button1 = 0;
    button2 = 0;
    selection = 0;
}

void mainMenu::Update(Uint32 dt) {
    timestamp += dt;
}

void mainMenu::LoadTextures() {
    titleTexture = LoadTexture("./title.png");
    button1 = LoadTexture("./button1.png");
    button2 = LoadTexture("./button2.png");
    selectTexture = LoadTexture("./selection.png");
}

void mainMenu::UnloadTextures() {
    UnloadTexture(titleTexture);
    UnloadTexture(button1);
    UnloadTexture(button2);
    UnloadTexture(selectTexture);
    titleTexture = 0;
    button1 = 0;
    button2 = 0;
}

void mainMenu::Draw() {
    //Set modelview matrix grid rotation according to the timestamp
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -15.0f);
    glRotatef(10.0f, 1.0f, 0.0f, 0.0f);
    glRotatef(timestamp * 0.02f, 0.0f, 1.0f, 0.0f);

    //Set the grid color
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glColor3f(0.0f, 0.5f, 1.0f);

    //Draw the menu background grid
    for (float i = -10.0f; i < 10.5f; i += 2.0f) {
        glBegin(GL_LINES);
        glVertex3f(i, 0.0f, -10.0f);
        glVertex3f(i, 0.0f, 10.0f);
        glEnd();
    }
    for (float i = -10.0f; i < 10.5f; i += 2.0f) {
        glBegin(GL_LINES);
        glVertex3f(-10.0f, 0.0f, i);
        glVertex3f(10.0f, 0.0f, i);
        glEnd();
    }

    //Reset the modelview matrix
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -3.62132f);

    //Bind menu title texture
    glColor3f(1.0f, 1.0f, 1.0f);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, titleTexture);

    //Draw the menu title
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-1.8f, 1.2f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(1.8f, 1.2f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(1.8f, 0.5f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-1.8f, 0.5f, 0.0f);
    glEnd();

    //Enable additive blending
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);

    //Draw the selection highlight
    if (selection != 0) {

        glColor3f(0.2f/*sin(timestamp * 0.002f) / 5.0f + 0.66f*/,
                  0.4f/*sin(timestamp * 0.002f) / 5.0f + 0.66f*/,
                  sin(timestamp * 0.002f) / 5.0f + 0.66f);
        glBindTexture(GL_TEXTURE_2D, selectTexture);

        glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(-0.5f, selection * -0.6f + 0.9f, 0.0f);
        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(0.5f, selection * -0.6f + 0.9f, 0.0f);
        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(0.5f, selection * -0.6f - 0.1f, 0.0f);
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(-0.5f, selection * -0.6f - 0.1f, 0.0f);
        glEnd();
    }

    //Draw the first main menu button
    glColor3f(0.0f, 0.5f, 1.0f);
    glBindTexture(GL_TEXTURE_2D, button1);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-0.3f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0.3f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(0.3f, -0.4f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-0.3f, -0.4f, 0.0f);
    glEnd();

    //Draw the second menu button
    glBindTexture(GL_TEXTURE_2D, button2);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-0.3f, -0.6f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0.3f, -0.6f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(0.3f, -1.0f, 0.0f);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-0.3f, -1.0f, 0.0f);
    glEnd();
}

void mainMenu::MouseClickEvent(int xCoordinate, int yCoordinate, bool right) {
}

void mainMenu::MouseReleaseEvent(int xCoordinate, int yCoordinate, bool right) {
    //If the user clicks the first button, go to the roster state
    if (selection == 1 && parent) {
        parent->SwitchState(GS_ROSTER);
    //Else quit the game
    } else if (selection == 2 && parent)
        parent->Quit();
}

void mainMenu::MouseMoveEvent(int newX, int newY, int relX, int relY) {
    //Set the selection number according to the mouse position
    if (newX > _WIDTH / 3 && newX <= _WIDTH * 2 / 3) {
        if (newY > _HEIGHT / 2 && newY <= _HEIGHT * 2 / 3)
            selection = 1;
        else if (newY > _HEIGHT * 2 / 3 && newY <= _HEIGHT * 5 / 6)
            selection = 2;
        else selection = 0;
    } else selection = 0;
}

void mainMenu::KeyPressEvent(SDLKey key, char c) {
    //Handle escape and enter keys
    switch (key) {
        case SDLK_ESCAPE:
            if (parent)
                parent->Quit();
            break;
        case SDLK_RETURN:
            if (selection == 2 && parent)
                parent->Quit();
            else if (selection == 1 && parent)
                parent->SwitchState(GS_GAME);
            break;
    }
}

void mainMenu::KeyReleaseEvent(SDLKey key) {}
